package io;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.JDOMException;

/**
 * This class provides access to ManHasRev data for the peer review system.
 * 
 * @author Lun-Wei Chang
 * 
 * @version March 11, 2014
 * 
 */
public class ManHasRevAccess extends AbstractAccess {
  /** Position of ManHasRec id element in the XML file. */
  public static final int MANREV_ID = 0;
  /** Position of ManHasRec summary element in the XML file. */
  public static final int MANUSCRIPT_ID = 1;
  /** Position of ManHasRec rationale element in the XML file. */
  public static final int REVIEW_ID = 2;
  
  /** Name of the XML's file. */
  private static final String XMLFILE = "Rec.xml";
  
  /** A constant for the ManHasRec id element in the XML file. */
  private static final String MANREVID = "ManRevID";
  /** A constant for the manuscript id element in the XML file. */
  private static final String MANUSCRIPTID = "ManuscriptID";
  /** A constant for the review id element in the XML file. */
  private static final String REVIEWID = "ReviewID";
  
  
  /** 
   * The constructor to instantiate the ManHasRevAccess object.
   * @throws IOException If there is a problem establishing connectivity to the file.
   * @throws JDOMException If the file cannot be correctly parsed. 
   */
  public ManHasRevAccess() throws IOException, JDOMException {
    super(XMLFILE);
  }
  
  /** 
   * Returns a list of review IDs based on the passed in Manuscript's ID.
   * @param the_paper a manuscript's id.
   * @return a list of review's ids. 
   */
  public List<Long> getReviewID(final long the_paper) {
    final List<Long> result = new ArrayList<Long>();
    final Element rootnode = getDoc().getRootElement();
    final List<Element> list =  rootnode.getChildren("ManRevID");
    for (int i = 0; i < list.size(); i++) {
      final Element node = (Element) list.get(i);
      final String rev_test = node.getChildText(REVIEWID);
      final long rev_num = Integer.parseInt(rev_test);
      if (the_paper == rev_num) {
        final String rev_id = node.getChildText("ReviewID");
        final long review_num = Integer.parseInt(rev_id);
      
        result.add(review_num);
      }
    }
    return result;
  }
}
